const app = getApp()
const { request } = require('../../utils/request.js');
const api = require('../../utils/api.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: 0,
    navBarHeight: 44,
    viewCount: 5621,
    articleList: [
      {
        id: 1,
        title: "如何准确拍照获取题目答案",
        category: "拍照技巧",
        date: "2023-06-15",
        isHot: true,
        isNew: false
      },
      {
        id: 2,
        title: "会员特权详细说明",
        category: "会员福利",
        date: "2023-06-20",
        isHot: true,
        isNew: true
      },
      {
        id: 3,
        title: "邀请好友获取免费次数",
        category: "邀请奖励",
        date: "2023-06-10",
        isHot: false,
        isNew: false
      },
      {
        id: 4,
        title: "常见问题解答大全",
        category: "常见问题",
        date: "2023-06-25",
        isHot: false,
        isNew: true
      },
      {
        id: 5,
        title: "如何提高解题准确率",
        category: "使用技巧",
        date: "2023-06-18",
        isHot: false,
        isNew: false
      }
    ],
    guideList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取状态栏高度
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight
    });

    // 模拟增加阅读量
    this.updateViewCount();

    this.getGuideList();
  },

  /**
   * 获取指南列表数据
   */
  getGuideList() {
    // 这里可以替换为真实的API请求
    const guideList = [
    ];
    request(api.articles, 'GET')
      .then(res => {
        console.log(res);
        this.setData({
          guideList:res.data.list || []
        });
      })
      .catch(err => {
        console.error('获取失败', err);
      });

    this.setData({
      guideList
    });
  },

  // 返回上一页
  goBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },

  // 查看文章详情
  viewDetail: function (e) {
    const articleId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/guide/detail/detail?id=' + articleId
    });
  },

  // 模拟更新阅读量
  updateViewCount: function () {
    const that = this;
    // 每10秒随机增加1-5的阅读量
    setInterval(function () {
      const increment = Math.floor(Math.random() * 5) + 1;
      that.setData({
        viewCount: that.data.viewCount + increment
      });
    }, 10000);
  },

  /**
   * 点击指南项
   */
  onGuideItemTap(e) {
    const url = e.currentTarget.dataset.url;
    if (url) {
      // 使用webview打开外部链接
      wx.navigateTo({
        url: `/pages/webview/webview?url=${encodeURIComponent(url)}`
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '新手指南 - 学法减分题库',
      path: '/pages/guide/guide'
    };
  }
})